import request from "../request.js"
import tabs from "../tabs.js"

let page = {
    after_build_view: function (view) {
        let page_info=this.page_info;
        let path = "datamodel/getrows";
        let params = {
            datamodel: 112,
            tablename: "bpm_process_def",
            orderby: "category",
            orderdir: "asc",
            perpage: 10000
        }
        request(path, params, function (info) {  
            let html="<section class='row' style='padding: 1.5rem; padding-left: 2.5rem;'>";      
            
            let pre_category;    
            for(let row of info.rows){
                let category=row.category;
                let id=row.id;
                let name=row.name;
                if(category!=pre_category){
                    if(pre_category){
                        html+="</div>"
                    }
                    html+="<h4 class='page-header'><i class='fa fa-caret-down'></i> "+category+"</h4>";
                    html+="<div class='row'>"
                    pre_category=category;
                }
                html+="<div class='col-md-3 col-sm-4 style='padding:0.3rem;'><a style='cursor:pointer;' class='process_def' data-id='"+id+"' data-name='"+name+"'> \
                    <i class='fa fa-pencil-square'></i> ("+id+") "+name+"</a></div>";
            }
            html+="</div>"
            html+="</section>";
            view.append(html);

            view.find(".process_def").click(function(){
                let id=$(this).data("id");
                let name=$(this).data("name");
                let tab_id = "process_trigger_" + id;
                let tab_label = '新流程(' + id + ')';
                let this_property = {
                    action: "trigger",
                    process_def_id: id,
                    process_def_name: name
                }
                tabs.open_by_page_type("tabs_container", tab_id, tab_label, "fa-file-o", "process_approve_page_type", this_property);
            })
        });
    }
};

export default page;